﻿using BYS.XiAnComDept.BussLogic;
using FirstFloor.ModernUI.Windows;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace BYS.XiAnComDept.App.Pages
{
    /// <summary>
    /// Interaction logic for CompanyEdit.xaml
    /// </summary>
    public partial class CompanyEdit : UserControl, IContent
    {
        BllFactory bllFactory = new BllFactory();
        Company Company = null;
        AddOrEdit FormStatus = AddOrEdit.Add;

        public CompanyEdit()
        {
            InitializeComponent();
        }

        #region IContent
        public void OnFragmentNavigation(FirstFloor.ModernUI.Windows.Navigation.FragmentNavigationEventArgs e)
        {
        }

        public void OnNavigatedFrom(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
        }

        public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            Dictionary<string, string> pars = e.Source.GetQuery();
            if (pars.Keys.Contains("ComGuid"))
            {
                Guid comGuid = Guid.Parse(pars["ComGuid"]);
                this.Company = bllFactory.Db.Companies
                    .Include(x=>x.Funds)
                    .SingleOrDefault(x => x.ComGuid == comGuid);
                if(this.Company == null)
                {
                    MessageBox.Show("单位数据不存在，请重新查询！", "错误");
                    NavigationCommands.GoToPage.Execute("/Pages/CompanyQuery.xaml", this);
                }
                else
                {
                    this.DataContext = this.Company;
                    this.FormStatus = AddOrEdit.Edit;
                }
            }
            else
            {
                this.Company = new Company();
                this.DataContext = this.Company;
                this.ButtonDelete.IsEnabled = false;
            }
        }

        public void OnNavigatingFrom(FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e)
        {
        }
        #endregion

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            ValidateForm validator = new ValidateForm();
            if (this.ShowError(validator.ValidControls(this.GridForm))) { return; }
            switch (this.FormStatus)
            {
                case AddOrEdit.Add:
                    bllFactory.Db.Companies.Add(this.Company);
                    bllFactory.Db.SaveChanges();
                    MessageBox.Show("单位信息添加成功！\n你可以继续修改其信息", "单位信息添加成功");
                    NavigationCommands.GoToPage.Execute(
                        "/Pages/CompanyEdit.xaml?ComGuid=" + this.Company.ComGuid,
                        this);
                    break;
                case AddOrEdit.Edit:
                    this.Company.CompanyPreName = this.Company.CompanyPreName.IfNull(string.Empty);
                    string currentCompanyName  = bllFactory.Db.Entry(this.Company).CurrentValues.GetValue<string>("CompanyName");
                    string originalCompanyName = bllFactory.Db.Entry(this.Company).OriginalValues.GetValue<string>("CompanyName");
                    if (originalCompanyName != currentCompanyName && !this.Company.CompanyPreName.Contains(originalCompanyName))
                    {
                        this.Company.CompanyPreName = this.Company.CompanyPreName.JoinWith(originalCompanyName, ",");
                    }
                    bllFactory.Db.SaveChanges();
                    this.SetDataContext(this.Company);
                    MessageBox.Show("单位信息修改成功！", "单位信息修改成功");
                    break;
            }
        }

        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (this.Company.Funds != null && this.Company.Funds.Count > 0)
            {
                MessageBox.Show("单位还有专项资金数据，如果确定要删除此单位，可进入专项资金管理界面，先删除专项资金数据，再尝试删除单位数据。！", "无法删除！");
                return;
            }
            if (MessageBox.Show("你确定要删除此单位数据呢？", "确定？", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                bllFactory.Db.Companies.Remove(this.Company);
                bllFactory.Db.SaveChanges();
                MessageBox.Show("单位删除成功！", "单位信息删除成功");
                NavigationCommands.GoToPage.Execute("/Pages/CompanyQuery.xaml", this);
            }
        }

    }
}
